package cn.com.self.apis.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public class CommonUtil {

    static String appKey="sfrz";
    static String appsecret="2EF1t+tCV0wUA5f1k09+yw==";
    /**
     * 获取当前URL
     * @param request
     * @return
     */
    public String getCurlUrl(HttpServletRequest request) {
        StringBuffer currUrl = new StringBuffer();
        String port = String.valueOf(request.getServerPort());
        String scheme = request.getScheme();
        if ("80".equals(port) || "8080".equals(port)) {
            port = "";
            scheme = "https";
        } else {
            port = ":" + port;
        }
        currUrl.append(scheme).append("://").append(request.getServerName())
                .append(port).append(request.getRequestURI());
        return currUrl.toString();
    }
    
    /**
     * 接口调用参数封装
     * @param cmd
     * @param param
     * @return
     */
    public static JSONObject paramsPackage(String cmd, Map<String,Object> param) {
        JSONObject object = new JSONObject();
        JSONObject params = new JSONObject();
        JSONObject common = new JSONObject();
        JSONObject inputData = new JSONObject();
        JSONObject user = new JSONObject();
        object.put("cmd", cmd);
        object.put("appKey", appKey);
        object.put("appSecret", appsecret);

        common.put("user", user);

        params.put("cmd", cmd);
        params.put("common", common);
        params.put("params", param);

        inputData.put("params", params);
        object.put("inputData", inputData);
        
        return object;
    }

    /**
     * 接口调用参数封装
     * @param cmd
     * @param param
     * @return
     */
    public static JSONObject paramsPackageCertification(String cmd, Map<String,Object> param) {
        JSONObject object = new JSONObject();
        JSONObject params = new JSONObject();
        JSONObject common = new JSONObject();
        JSONObject inputData = new JSONObject();
        JSONObject user = new JSONObject();
        object.put("cmd", cmd);
        object.put("appKey", "authentication");
        object.put("appSecret", "23131ca7c6dbe6bd9c52f0144e497326");

        common.put("user", user);

        params.put("cmd", cmd);
        params.put("common", common);
        params.put("params", param);

        inputData.put("params", params);
        object.put("inputData", inputData);

        return object;
    }
    
    /**
     * 接口调用参数封装--新增用户参数
     * @param cmd
     * @param param
     * @param user
     * @return
     */
    public static JSONObject paramsPackage_user(String cmd, Map<String,Object> param,Map<String,Object> user) {
        JSONObject object = new JSONObject();
        JSONObject params = new JSONObject();
        JSONObject common = new JSONObject();
        JSONObject inputData = new JSONObject();
        object.put("cmd", cmd);
        object.put("appKey", appKey);
        object.put("appSecret",appsecret);

        common.put("user", user);

        params.put("cmd", cmd);
        params.put("common", common);
        params.put("params", param);

        inputData.put("params", params);
        object.put("inputData", inputData);
        
        return object;
    }
    /**
     * 接口返回的请求处理信息
     * @param result
     * @return
     */
    public static Map<String, String> resultInfo(String result) {
        Map<String, String> resultInfo = new HashMap<String, String>();
        JSONObject resultObj = JSON.parseObject(result);
        resultInfo.put("returnCode", resultObj.getString("returnCode"));
        resultInfo.put("cmd", resultObj.getString("cmd"));
        resultInfo.put("exception", resultObj.getString("exception"));
        resultInfo.put("returnMessage", resultObj.getString("returnMessage"));
        resultInfo.put("data", resultObj.getString("data"));
        
        return resultInfo;
    }

    @SuppressWarnings("unchecked")
	public static Map<String, Object> getParameterMap(HttpServletRequest request) {
        Map<String, String[]> properties = request.getParameterMap();//把请求参数封装到Map<String, String[]>中
        Map<String, Object> returnMap = new HashMap<String, Object>();
        Iterator<Map.Entry<String, String[]>> iter = properties.entrySet().iterator();
        String name = "";
        String value = "";
        while (iter.hasNext()) {
            Map.Entry<String, String[]> entry = iter.next();
            name = entry.getKey();
            Object valueObj = entry.getValue();
            if (null == valueObj) {
                value = "";
            } else if (valueObj instanceof String[]) {
                String[] values = (String[]) valueObj;
                for (int i = 0; i < values.length; i++) { //用于请求参数中有多个相同名称
                    value = values[i] + ",";
                }
                value = value.substring(0, value.length() - 1);
            } else {
                value = valueObj.toString();//用于请求参数中请求参数名唯一
            }
            returnMap.put(name, value);
        }
        return returnMap;
    }
    //返回值类型为Map<String, String>
    @SuppressWarnings("unchecked")
	public static Map<String, String> getParameterStringMap(HttpServletRequest request) {
        Map<String, String[]> properties = request.getParameterMap();//把请求参数封装到Map<String, String[]>中
        Map<String, String> returnMap = new HashMap<String, String>();
        String name = "";
        String value = "";
        for (Map.Entry<String, String[]> entry : properties.entrySet()) {
            name = entry.getKey();
            String[] values = entry.getValue();
            if (null == values) {
                value = "";
            } else if (values.length>1) {
                for (int i = 0; i < values.length; i++) { //用于请求参数中有多个相同名称
                    value = values[i] + ",";
                }
                value = value.substring(0, value.length() - 1);
            } else {
                value = values[0];//用于请求参数中请求参数名唯一
            }
            returnMap.put(name, value);

        }
        return returnMap;
    }
}
